unit SmallStruct.Core.TSSClassTable;

interface

uses Classes,
  SmallStruct.Core,
  SmallStruct.Core.TSSObject;

type
  TSSClassTable = class(TSSObject, ISSClassTable)
  public
    _ClassNameList: TStringList;
    _ClassList: TList;
  private
    function QueryClass(const AClassName: string): TComponentClass;
    procedure RegisterClass(const AClassName: string; AClass: TComponentClass);
    procedure UnregisterClass(const AClassName: string);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses SmallStruct.Error;

{ TSSClassTable }

constructor TSSClassTable.Create(AOwner: TComponent);
begin
  inherited;
  _ClassNameList := TStringList.Create;
  _ClassList := TList.Create;
end;

destructor TSSClassTable.Destroy;
begin
  _ClassNameList.Free;
  _ClassList.Free;

  inherited;
end;

function TSSClassTable.QueryClass(const AClassName: string): TComponentClass;
begin
  if _ClassNameList.IndexOf(AClassName) >= 0 then
    Result := _ClassList.Items[_ClassNameList.IndexOf(AClassName)]
  else
    raise ESSClassNotFound.Create(ClassName, 'QueryClass', AClassName);
end;

procedure TSSClassTable.RegisterClass(const AClassName: string; AClass: TComponentClass);
begin
  if _ClassNameList.IndexOf(AClassName) >= 0 then
    raise ESSClassExists.Create(ClassName, 'QueryClass', AClassName)
  else
  begin
    _ClassNameList.Add(AClassName);
    _ClassList.Add(AClass);
  end;
end;

procedure TSSClassTable.UnregisterClass(const AClassName: string);
begin
  _ClassList.Delete(_ClassNameList.IndexOf(AClassName));
  _ClassNameList.Delete(_ClassNameList.IndexOf(AClassName));
end;

end.
